<?php

namespace app\model;

use think\Model;

class DrawRecord extends Model
{
    // 表名
    protected $name = 'lucky_draw_record';
    // 主键
    protected $pk = 'id';
    // 自动写入时间戳
    protected $autoWriteTimestamp = 'timestamp';

    public function prize()
    {
        return $this->hasOne(Prize::class, 'id', 'prize_id');
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public static function getList($page = 1, $limit = 10, $where = [], $order = ['id' => 'asc'])
    {
        $query = self::where($where);

        // 获取总条数
        $total = $query->count();

        // 获取当前页数据
        $list = $query->with(['prize', 'user'])
            ->order($order)
            ->page($page, $limit)
            ->select();

        // 判断是否有下一页
        $hasMore = ($page * $limit) < $total;

        return [
            'list' => $list,
            'total' => $total,
            'hasMore' => $hasMore
        ];
    }


    /**
     * 记录抽奖结果（单活动模式）
     */
    public static function recordDraw($userId, $prizeId = null, $round = 0)
    {
        return self::create([
            'user_id' => $userId,
            'prize_id' => $prizeId,
            'round' => $round
        ]);
    }
}
